<?php
namespace Admin\Controller;

class UsersController extends CommonController{

    /**
    * 用户列表
    */
    public function index(){
        $this->display();
    }

    public function edit(){
        if($_GET['uid']){
            $info = D('Users')->find($_GET['uid']);
            if($info['role'] == 'ROLE_SUPER_ADMIN'){
                echo "<p style=\"padding:10px 30px;font-size:16px;color:red;\">注意：超级管理员不能被编辑</p>";
            }else{
                $this->info = $info;
            }
        }
        $this->display();
    }

    public function edit_post(){
        if(IS_POST){
            
            if(!$_POST['status']){
                $_POST['status'] = -1; //设定状态，-1表示禁用用户
            }
            if($_POST['password']){
                $_POST['password'] = md5($_POST['password']);
            }else{
                if($_POST['uid']){
                    $_POST['password'] = md5('123456');
                }else{
                    unset($_POST['password']); //如果为空的话，密码重置
                }
            }
            $_POST['update_time'] = Date('Y-m-d H:i:s');

            $m = D('Users');

            //检测合法性
            if($_POST['uid']){
                if($_POST['username']){
                    $tmpUid = $m->where('username = "'.$_POST['username'].'"')->getField('uid');
                    if($tmpUid != $_POST['uid'] && $tmpUid){
                        _apiReturn(0,"用户名已经存在，不能使用该用户名",$m->_sql());
                    }
                }
                if($_POST['mobile']){
                    $tmpUid = $m->where('mobile = "'.$_POST['mobile'].'"')->getField('uid');
                    if($tmpUid != $_POST['uid'] && $tmpUid){
                        _apiReturn(0,"手机号码已经存在，不能使用该手机号码");
                    }
                }
                if($_POST['email']){
                    $tmpUid = $m->where('email = "'.$_POST['email'].'"')->getField('uid');
                    if($tmpUid != $_POST['uid'] && $tmpUid){
                        _apiReturn(0,"Email已经存在，不能使用该Email");
                    }
                }
                if($_POST['p1']){
                    $tmpUid = $m->where('p1 = "'.$_POST['p1'].'"')->getField('uid');
                    if($tmpUid != $_POST['uid'] && $tmpUid){
                        _apiReturn(0,"客户代码已经被使用，请换其他！");
                    }
                }
            }else{
                if($_POST['p1']){
                    $tmpUid = $m->where('"p1" = "'.$_POST['p1'].'"')->getField('uid');
                    if($tmpUid){
                        _apiReturn(0,"客户代码已经被使用，请换其他！");
                    }
                }
            }

            $data = $m->create($_POST);
            if (!$data){    
                 // 如果创建失败 表示验证没有通过 输出错误提示信息    
                  $this->error($m->getError());
            }
            else{    
                if($_POST['uid']){
                    
                    if($m->save($data)){
                        $this->success(L('success'));
                    }else{
                        $this->error(L('failed'));
                    }
                }else{
                    
                    $data['create_time'] = $data['update_time'];
                    if($m->add($data)){
                        $this->success(L('success'));
                    }else{
                        $this->error(L('failed'));
                    }
                }
            }
        }
    }

    public function getdata(){
        
        $p = I('p') ? I('p') : 1;
        $row = I('row') ? I('row') : 10;

        $m = D('Users');
        if(I('condition')) {
            $condition = condition_to_array(I('condition')); //加入条件参数
            foreach($condition as $key=>$val){
                if($key == 'uid' || $key == 'role'){
                    $map[$key] = $val;
                }else{
                    $map[$key] = array('like',"%{$val}%");
                }
            }
        }
        $data['data'] = $m->where($map)->page($p,$row)->select();
        //$data['sql'] = $m->_sql();
		$data['page']['total'] = $m->where($map)->count();
		$data['page']['cur'] = $p;
		$data['page']['row'] = $row;
		
		_apiReturn(1, L('success'), $data, $url); //数据获取成功
    }



    /**
    * 用户角色管理
    */
    public function role(){
        $this->display();
    }

    public function get_role_data(){
        $p = I('p') ? I('p') : 1;
        $row = I('row') ? I('row') : 10;

        $m = D('UserRoles');
        if(I('condition')) $map = condition_to_array(I('condition')); //加入条件参数
        $data['data'] = $m->where($map)->page($p,$row)->select();
		$data['page']['total'] = $m->where($map)->count();
		$data['page']['cur'] = $p;
		$data['page']['row'] = $row;
		
		_apiReturn(1, L('success'), $data, $url); //数据获取成功
    }

    public function role_edit(){
        if($_GET['id']){
            $this->info = M('UserRoles')->find($_GET['id']);
        }else{
			$info['json'] = '{}';
			$this->info = $info;
		}
        $this->display();
    }

    /**
    * 编辑角色
    */
    public function role_edit_post(){
        if(IS_POST){
            $_POST['update_time'] = Date('Y-m-d H:i:s'); //更新时间
            $m = M('UserRoles');
            $tmpData = $m->create($_POST);
            if($tmpData['id']){
                if($m->save($tmpData)){
                    $this->success('success');
                }else{
                    $this->error('error');
                }
            }else{
                if($m->add($tmpData)){
                    $this->success('success');
                }else{
                    $this->error('error');
                }
            }
        }
    }

    /**
    * 角色删除
    */
    public function role_del(){
        $this->_del(M('UserRoles'),$_REQUEST['ids']);
    }

    /**
    * 管理员管理
    */
    public function admin(){
        $this->display();
    }

    public function del(){
        if(I('ids') == 1){
            _apiReturn(0,"顶级超级管理员不能删除！");
        }
        $m = M('Users');
        $map['uid'] = I('ids');
        $user = $m->where($map)->find();
        if($user['status'] == 1){
            _apiReturn(0,'删除前需要将用户禁用！');
        }
        $this->_del($m,I('ids'));
    }
}